package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
//@Transactional mongodb事务必须复制集方式
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Region region) {
        region.setId(null);
        repository.save(region);
    }

    @Override
    public void update(Region region) {
        repository.save(region);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Region get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Region> list() {
        return repository.findAll();
    }

    @Override
    public Page<Region> query(RegionQuery qo) {
        //该方法针对全部数据进行分页
        //repository.findAll(PageRequest.of(1,3));

        //-----------------------------------------

        //只做查询,不做分页
        //template

        //mongodb的分页只能自己拼接分页逻辑

        Query query = new Query();
        //总条数
       /* long count = template.count(query, Region.class);
        if(count == 0){
            return Page.empty();
        }*/
        //当前页数据
        /*Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Region> list = template.find(query,Region.class);

        return new PageImpl<>(list,pageable,count);*/

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");

        return DBHelper.query(template,pageable,query,Region.class,qo);
    }

    @Override
    public void saveOrUpdate(Region region) {

        if(StringUtils.hasLength(region.getId())){
            //update
            //特殊案例, 页面更新的数据, 刚好跟集合中字段个数一致
            //所以这种等价于全量更新
            this.update(region);
        }else{
            //save
            this.save(region);
        }
    }


    @Override
    public void changeHotValue(String id, int hot) {
        //1:查询
        //Region region = this.get(id);
        //2:替换
        //region.setIshot(hot);
        //3:更新
       //this.update(region);

        //部分字段更新
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));

        Update update = new Update();
        update.set("ishot",hot);

        template.updateMulti(query,update,Region.class);
    }

    @Override
    public List<Region> queryHotRegion() {

        //1:状态是热门
        //2:通过序号排序
        return repository.findByIshotOrderBySequence(Region.STATE_HOT);
    }


}
